#include <iostream>
using namespace std;
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <fcntl.h>
#include <string>
#include <json-c/json_object.h> /* json */
#include "stdShared.h"

/* 服务端口 */
#define SERVER_PORT 8080
#define SIZE 20
#define BUFFER_SIZE 128

void signUpLoginInterFace()
{
    cout << "1.注册" << endl;
    cout << "2.登陆" << endl;
}
struct userInfo
{
    int type; // 1.注册 2.登陆
    char name[SIZE];
    char passwd[SIZE];
};

/* 注册信息 */
void registerInfo(int sockfd)
{
    string username;
    cout << "请输入你的姓名：";
    cin >> username;

    string password;
    cout << "请输入你的密码：";
    cin >> password;

#if 0
    userInfo info;
    /* 清空脏数据 */
    memset(&info, 0, sizeof(info));
    info.type = 1;
    strncpy(info.name, username.c_str(), sizeof(info.name) - 1);
    strncpy(info.passwd, password.c_str(), sizeof(info.passwd) - 1);

    /* 发送到服务器 */
    write(sockfd, &info, sizeof(info));
#endif

    /* {}叫做json对象 */
    /* 1.创建json对象 */
    json_object *jsonObj = json_object_new_object();

    /* 2.向json对象添加<key,value>键值对 */
    json_object_object_add(jsonObj, "type", json_object_new_int(REGISTER));
    json_object_object_add(jsonObj, "name", json_object_new_string(username.c_str()));
    json_object_object_add(jsonObj, "passwd", json_object_new_string(password.c_str()));
    /* 将json对象转换成json字符串 */
    const char *jsonStr = json_object_to_json_string(jsonObj);
    cout << "jsonStr:" << jsonStr << endl;

    /* 4.发送到服务器 */
    write(sockfd, jsonStr, strlen(jsonStr));
}

/* 登陆信息 */
void loginInfo(int sockfd)
{
    string username;
    cout << "请输入你的姓名：";
    cin >> username;

    string password;
    cout << "请输入你的密码：";
    cin >> password;

#if 0
    userInfo info;
    /* 清空脏数据 */
    memset(&info, 0, sizeof(info));
    info.type = 2;
    strncpy(info.name, username.c_str(), sizeof(info.name) - 1);
    strncpy(info.passwd, password.c_str(), sizeof(info.passwd) - 1);

    /* 发送到服务器 */
    write(sockfd, &info, sizeof(info));
#endif

    /* {}叫做json对象 */
    /* 1.创建json对象 */
    json_object *jsonObj = json_object_new_object();

    /* 2.向json对象添加<key,value>键值对 */
    json_object_object_add(jsonObj, "type", json_object_new_int(LOGIN));
    json_object_object_add(jsonObj, "name", json_object_new_string(username.c_str()));
    json_object_object_add(jsonObj, "passwd", json_object_new_string(password.c_str()));
    /* 将json对象转换成json字符串 */
    const char *jsonStr = json_object_to_json_string(jsonObj);
    cout << "jsonStr:" << jsonStr << endl;

    /* 4.发送到服务器 */
    write(sockfd, jsonStr, strlen(jsonStr));
}

int main()
{
    /* 创建套接字 */
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        perror("socket error:");
        exit(-1);
    }
    cout << "sockfd:" << sockfd << endl;

    /* 连接服务器 */
    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(SERVER_PORT);
    const char *serverIp = "192.168.15.128";
    inet_pton(AF_INET, serverIp, &(serverAddr.sin_addr.s_addr));

    int ret = connect(sockfd, reinterpret_cast<const sockaddr *>(&serverAddr), sizeof(serverAddr));
    if (ret != 0)
    {
        perror("connect error:");
        exit(-1);
    }

    /* 程序执行到这个地方：连接服务器成功... */
    signUpLoginInterFace();

    char readBuffer[BUFFER_SIZE];

    int choice = 0;

    while (1)
    {
        /* 清空缓冲区 */
        memset(readBuffer, 0, sizeof(readBuffer));

        cout << "请输入你的选择...";

        cin >> choice;

        switch (choice)
        {
        case 1:
            registerInfo(sockfd);
            break;
        case 2:
            loginInfo(sockfd);
            break;

        default:
            break;
        }
    }

    return 0;
}

